from django.db import models
from gis.common.django_ext.models import BaseModel, ListField


class User(BaseModel):
    name = models.CharField(max_length=50, unique=True)
    password = models.CharField(max_length=100)
    is_super = models.BooleanField(default=False)
    enable = models.BooleanField(default=True)
    roles = models.ManyToManyField('Role')
    deleted = models.BooleanField(default=False)
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, blank=True, null=True)
    last_login_at = models.DateTimeField(null=True)  # 最后登录时间


class Role(BaseModel):
    name = models.CharField(max_length=50, unique=True)
    desc = models.CharField(max_length=200, null=True)
    permissions = models.ManyToManyField('Permission', through='RolePermissionRel')
    can_update = models.BooleanField(default=True)


class Permission(BaseModel):
    parent = models.ForeignKey('self', on_delete=models.PROTECT, null=True)
    name = models.CharField(max_length=50)
    code = models.CharField(max_length=50, unique=True)
    sort_index = models.IntegerField(default=1)
    desc = models.CharField(max_length=200, null=True)
    full_path = models.CharField(max_length=100)
    is_leaf = models.BooleanField(default=False)
    fields = ListField(max_length=1000, null=True)

    class Meta:
        ordering = ['sort_index']


class RolePermissionRel(BaseModel):
    role = models.ForeignKey(Role, on_delete=models.CASCADE)
    permission = models.ForeignKey(Permission, on_delete=models.CASCADE)
    include_fields = ListField(max_length=1000, null=True)

    class Meta:
        unique_together = ('role', 'permission')


class Record(BaseModel):
    resource = models.CharField(max_length=100)
    resource_id = models.IntegerField()
    action = models.SmallIntegerField()
    content = models.TextField()
    operator = models.IntegerField()
    ip = models.CharField(max_length=100)
    user_agent = models.CharField(max_length=100)
